package com.lzq.course.server.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzq.course.server.domain.Course;
import com.lzq.course.server.domain.CourseCategory;
import com.lzq.course.server.domain.CourseCategoryExample;
import com.lzq.course.server.dto.CategoryDto;
import com.lzq.course.server.dto.CourseCategoryDto;
import com.lzq.course.server.dto.PageDto;
import com.lzq.course.server.mapper.CourseCategoryMapper;
import com.lzq.course.server.util.CopyUtil;
import com.lzq.course.server.util.UuidUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class CourseCategoryService {

    @Resource
    private CourseCategoryMapper courseCategoryMapper;

    /**
     * example 这个文件很有用
     * 可以帮我们生成where order 查询
     * 不用单独写sql的增删改查
     */
    public void list(PageDto<CourseCategoryDto> pageDto) {
        // 分页查询 页码不是从0开始
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());

        // 排序
        CourseCategoryExample courseCategoryExample = new CourseCategoryExample();

        // 查询
        List<CourseCategory> courseCategoryList = courseCategoryMapper.selectByExample(courseCategoryExample);

        PageInfo<CourseCategory> pageInfo = new PageInfo<>(courseCategoryList);
        pageDto.setTotal(pageInfo.getTotal());

        List<CourseCategoryDto> courseCategoryDtoList = CopyUtil.copyList(courseCategoryList, CourseCategoryDto.class);

        pageDto.setList(courseCategoryDtoList);
    }

    /**
     * 保存 修改
     * @param courseCategoryDto
     */
    public void save(CourseCategoryDto courseCategoryDto) {
        //BeanUtils.copyProperties(courseCategoryDto, courseCategory);
        CourseCategory copyCourseCategory = CopyUtil.copy(courseCategoryDto, CourseCategory.class);

        if (StringUtils.isEmpty(courseCategoryDto.getId())) {
            // 新增
            // 八位的 id
            copyCourseCategory.setId(UuidUtil.getShortUuid());

            Date now = new Date();

            courseCategoryMapper.insert(copyCourseCategory);
        } else {
            // 编辑
            courseCategoryMapper.updateByPrimaryKey(copyCourseCategory);
        }
    }

    /**
     * 删除
     * @param id
     */
    public void delete(String id) {
        courseCategoryMapper.deleteByPrimaryKey(id);
    }

    /**
     * 批量课程关联分类
     * @param courseId
     * @param dtoList
     */
    @Transactional
    public void saveBach(String courseId, List<CategoryDto> dtoList) {
        // 编辑的话 之前应该清除旧的分类
        CourseCategoryExample example = new CourseCategoryExample();
        example.createCriteria().andCourseIdEqualTo(courseId);
        courseCategoryMapper.deleteByExample(example);

        for (int i = 0; i < dtoList.size(); i++) {
            CategoryDto categoryDto = dtoList.get(i);
            CourseCategory courseCategory = new CourseCategory();
            courseCategory.setCourseId(courseId);
            courseCategory.setCategoryId(categoryDto.getId());
            courseCategory.setId(UuidUtil.getShortUuid());
            courseCategoryMapper.insert(courseCategory);
        }
    }

    /**
     * 查询课程下的分类
     * @param courseId
     * @return
     */
    public List<CourseCategoryDto> listByCourse(String courseId) {
        CourseCategoryExample example = new CourseCategoryExample();
        example.createCriteria().andCourseIdEqualTo(courseId);
        List<CourseCategory> courseCategories = courseCategoryMapper.selectByExample(example);
        return CopyUtil.copyList(courseCategories, CourseCategoryDto.class);
    }
}
